package cn.jbolt.ec.retailer;


import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecMember;
import cn.jbolt.common.model.BecRetailerStaff;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

public class RetailerStaffService extends JBoltBaseService<BecRetailerStaff> {
    private BecRetailerStaff dao = new BecRetailerStaff().dao();
    @Override
    protected BecRetailerStaff dao() {
        return dao;
    }


    /**
     * 获取 员工管理 首页信息
     * @param pageNum
     * @param pageSize
     * @param retailer_id
     * @param keywords
     * @return
     */
    public Page<BecRetailerStaff> get_retailer_staff_paginate(Integer pageNum, Integer pageSize,
                                                              Integer retailer_id, String keywords){
        Kv paras=Kv.by("table",table());
        paras.setIfNotNull("retailer_id",retailer_id);
        paras.setIfNotNull("keywords",keywords);
        return paginateBySqlTemplate("ec.mall.manage.retailer.get_retaile_staff_page",paras,pageNum,pageSize);
    }



    public Ret save_staff(BecRetailerStaff becRetailerStaff, Integer member_id ,String name){
        if (becRetailerStaff == null || notOk(member_id) || name == null)
            return fail("参数传入错误");
        BecMember becMember_dao = new BecMember().dao();
        BecMember becMember = becMember_dao.findById(member_id);
        if (becMember == null)
            return fail("操作失败");


        becRetailerStaff.setId(becMember.getId());
        becRetailerStaff.setName(name);
        becRetailerStaff.setOpenid(becMember.getOpenidJmd());
        becRetailerStaff.setMobile(becMember.getMobile());

        BecRetailerStaff new_becRetailerStaff = findFirst(Kv.by("openid",becRetailerStaff.getOpenid()));



        boolean success ;


        if (new_becRetailerStaff == null)
            success = becRetailerStaff.save();

        else if (exists("openid",becRetailerStaff.getOpenid()) && !new_becRetailerStaff.getIsDel())
            return fail("该员工已在其他商店！！请重新选择");

        else if (exists("openid",becRetailerStaff.getOpenid()) && new_becRetailerStaff.getIsDel())
           {


               new_becRetailerStaff.setMobile(becMember.getMobile());
               new_becRetailerStaff.setOpenid(becMember.getOpenidJmd());
               new_becRetailerStaff.setName(name);

               new_becRetailerStaff.setRetailerId(becRetailerStaff.getRetailerId());
               new_becRetailerStaff.setIsDel(false);


               // 由于 前端时间的 数据加入问题  所以修改时需要判断！
               if (becRetailerStaff.getId() == new_becRetailerStaff.getId())
                   success = becRetailerStaff.update();
               else{
                   new_becRetailerStaff.delete();
                   success = becRetailerStaff.save();
               }

           }

        else
            success = false;



        if (success)
            return  SUCCESS;
        return FAIL;

    }


    public Ret delete_staff(Integer id){
        if (notOk(id))
            return fail("操作错误！");
        Ret ret=toggleBoolean(id, "is_del");
        if (ret.isOk())
            return SUCCESS;
        return FAIL;
    }



    public boolean judge_openid(String open_id){
        List<BecRetailerStaff> becRetailerStaffs = find("select openid,is_del from bec_retailer_staff where openid=?",open_id);
        if (becRetailerStaffs == null)
            return false;
        for (BecRetailerStaff becRetailerStaff : becRetailerStaffs){
            if (! becRetailerStaff.getIsDel())
                return  true;
        }
        return false;
    }

}

